package name.elftesting.vncelf;

/**
 * 
 * @author Xiaoping Wu
 *
 */
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JComponent;

import name.elftesting.vnctest.JavaFormatScript;
import name.elftesting.vnctest.LanguageTemplete;
import name.elftesting.vnctest.VncElf;

import org.sikuli.script.*;

import java.awt.*;

class ToolBar implements ActionListener {

	VncViewer viewer;
	JButton disconnectButton;
	JButton optionsButton;
	JButton recordscreenButton;
	JButton playScreenButton;
	JButton clipboardButton;
	JButton ctrlAltDelButton;
	JButton refreshButton;
	JButton captureScreenButton;
	JButton	clearLog;
	JButton open;
	JButton save;
	JButton run;
	JButton stopRun;
	// JButton pause;
	// JButton resume;
	JToggleButton recordAction;
	JButton stopRecordAction;
	JToolBar toolBar;
	Thread running;
	JButton saveas;
	private String saveFilePath = "";
	public ImageIcon getImage(String path) {
		ImageIcon image = new ImageIcon(this.getClass().getResource(path));
		
		return image;
	}

	public JToolBar createToolBar() {
		toolBar = new JToolBar();
		//toolBar.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));

		open = new JButton();
		String imagePath = "/images/launch.jpg";

		open.setIcon(this.getImage("/images/open.png"));
		open.setToolTipText(ActionConstant.OPEN);
		open.setEnabled(false);
		toolBar.add(open);
		open.addActionListener(this);
		save = new JButton();
		save.setIcon(this.getImage("/images/save.png"));
		save.setToolTipText(ActionConstant.SAVE);
		save.setEnabled(false);
		toolBar.add(save);
		save.addActionListener(this);

		saveas = new JButton();
		saveas.setIcon(this.getImage("/images/saveas.png"));
		saveas.setToolTipText(ActionConstant.SAVEAS);
		saveas.setEnabled(false);
		toolBar.add(saveas);
		saveas.addActionListener(this);


		clipboardButton = new JButton();
		clipboardButton.setIcon(this.getImage("/images/copy.png"));
		clipboardButton.setToolTipText(ActionConstant.CLIPBOARD);
		clipboardButton.setEnabled(false);
		toolBar.add(clipboardButton);
		clipboardButton.addActionListener(this);
		
		refreshButton = new JButton();
		refreshButton.setIcon(this.getImage("/images/refresh.png"));
		refreshButton.setToolTipText(ActionConstant.REFRESH);
		refreshButton.setEnabled(false);
		toolBar.add(refreshButton);
		refreshButton.addActionListener(this);

		clearLog = new JButton();
		clearLog.setIcon(this.getImage("/images/clearlog.png"));
		clearLog.setToolTipText(ActionConstant.CLEARLOG);
		clearLog.setEnabled(false);
		toolBar.add(clearLog);
		clearLog.addActionListener(this);
		
		run = new JButton(this.getImage("/images/run.png"));
		run.setToolTipText(ActionConstant.START);
		run.setEnabled(false);
		toolBar.add(run);
		run.addActionListener(this);

		stopRun = new JButton(this.getImage("/images/stop.png"));
		stopRun.setToolTipText(ActionConstant.STOP);
		stopRun.setEnabled(false);
		toolBar.add(stopRun);
		stopRun.addActionListener(this);

		// pause = new JButton(new ImageIcon("images\\pause.png"));
		// pause.setToolTipText(ActionConstant.PAUSE);
		// pause.setEnabled(true);
		// toolBar.add(pause);
		// pause.addActionListener(this);
		//
		// resume = new JButton(new ImageIcon("images\\resume.png"));
		// resume.setToolTipText(ActionConstant.RESUME);
		// resume.setEnabled(true);
		// toolBar.add(resume);
		// resume.addActionListener(this);

		captureScreenButton = new JButton();
		captureScreenButton.setIcon(this.getImage("/images/capture.png"));
		captureScreenButton.setToolTipText(ActionConstant.CAPSCREEN);
		captureScreenButton.setEnabled(false);
		toolBar.add(captureScreenButton);
		captureScreenButton.addActionListener(this);

		recordAction = new JToggleButton(this.getImage("/images/record.png"));
		recordAction.setToolTipText("record action");
		recordAction.setEnabled(false);
		toolBar.add(recordAction);
		recordAction.addActionListener(this);
		
		if (viewer.rec != null) {
			recordscreenButton = new JButton();
			recordscreenButton.setIcon(this
					.getImage("/images/recordscreen.png"));
			recordscreenButton.setEnabled(false);
			recordscreenButton.setToolTipText(ActionConstant.RE_SCREEN);
			toolBar.add(recordscreenButton);
			recordscreenButton.addActionListener(this);
		}

		playScreenButton = new JButton();
		playScreenButton.setIcon(this.getImage("/images/playerscreen.png"));
		playScreenButton.setToolTipText(ActionConstant.PLAY_SCREEN);
		playScreenButton.setEnabled(false);
		toolBar.add(playScreenButton);
		playScreenButton.addActionListener(this);

		optionsButton = new JButton();
		optionsButton.setIcon(this.getImage("/images/options.png"));
		optionsButton.setToolTipText(ActionConstant.CONFIG);
		optionsButton.setEnabled(false);
		toolBar.add(optionsButton);
		optionsButton.addActionListener(this);

		ctrlAltDelButton = new JButton();
		ctrlAltDelButton.setIcon(this.getImage("/images/cad.png"));
		ctrlAltDelButton.setToolTipText(ActionConstant.SENDCAD);
		ctrlAltDelButton.setEnabled(false);
		toolBar.add(ctrlAltDelButton);
		ctrlAltDelButton.addActionListener(this);

		disconnectButton = new JButton();
		disconnectButton.setIcon(this.getImage("/images/disconnect.png"));
		disconnectButton.setToolTipText(ActionConstant.DIS_ACTION);
		disconnectButton.setEnabled(true);
		toolBar.add(disconnectButton);
		disconnectButton.addActionListener(this);
		return toolBar;
	}

	ToolBar(VncViewer v) {
		viewer = v;

	}

	//
	// Enable buttons on successful connection.
	//

	public void enableButtons() {
		disconnectButton.setEnabled(true);
		clipboardButton.setEnabled(true);
		refreshButton.setEnabled(true);
		Component[] cps=toolBar.getComponents();
		for(int i=0;i<cps.length;i++){
			((JComponent)cps[i]).setEnabled(true);
		}
	}

	//
	// Disable all buttons on disconnect.
	//

	public void disableButtonsOnDisconnect() {
		toolBar.remove(disconnectButton);
		disconnectButton = new JButton("Hide desktop");
		disconnectButton.setEnabled(true);
		toolBar.add(disconnectButton, 0);
		disconnectButton.addActionListener(this);

		optionsButton.setEnabled(false);
		clipboardButton.setEnabled(false);
		ctrlAltDelButton.setEnabled(false);
		refreshButton.setEnabled(false);

		toolBar.validate();
	}

	//
	// Enable/disable controls that should not be available in view-only
	// mode.
	//

	public void enableRemoteAccessControls(boolean enable) {
		ctrlAltDelButton.setEnabled(enable);
	}

	//
	// Event processing.
	//

	public void doOpen() {

		try {
			JFileChooser jfc = new JFileChooser(this.saveFilePath);
			FileFilter ffc = new FileNameExtensionFilter("vrs", "VRS",
					"vrs file");
			jfc.addChoosableFileFilter(ffc);

			int i = jfc.showOpenDialog(viewer);
			File file = null;
			if (i == JFileChooser.APPROVE_OPTION) {
				file = jfc.getSelectedFile();
			}

			FileInputStream os;
			try {

				BufferedReader br = new BufferedReader(new FileReader(file));
				String line = "";
				viewer.editor.setText("");
				while ((line = br.readLine()) != null) {
					viewer.editor.append(line + "\n");
					this.saveFilePath = file.getPath();
				}
			} catch (Exception e) {
				//JOptionPane.showMessageDialog(viewer, "Open file failed!");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} catch (Exception exe) {
			exe.printStackTrace();
		}
	}

	public void doSaveAs() {

		try {
			JFileChooser jfc = new JFileChooser(this.saveFilePath);
			FileFilter ffc = new FileNameExtensionFilter("vrs", "VRS",
					"vrs file");
			jfc.addChoosableFileFilter(ffc);

			int i = jfc.showSaveDialog(viewer);
			if (i == JFileChooser.APPROVE_OPTION) {
				File file = jfc.getSelectedFile();
				String about = "VRS";
				String ext = file.toString().toLowerCase();
				javax.swing.filechooser.FileFilter ff = jfc.getFileFilter();
				if (ff.equals(ffc)) {
					if (!ext.endsWith(".vrs")) {
						String ns = ext + ".vrs";
						file = new File(ns);
						about = "vrs";
					}
				}

				try {
					if (!file.exists())
						file.createNewFile();
					String[] lines = viewer.editor.getText().trim().split("\n");

					PrintWriter out = new PrintWriter(
							new FileOutputStream(file), true);
					for (String line : lines)
						out.println(line);

					this.saveFilePath = file.getPath();
				} catch (Exception e) {
					JOptionPane.showMessageDialog(viewer, "Save failed!");
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		} catch (Exception exe) {
			exe.printStackTrace();
		}
	}

	private void doSave() {
		try {
			File file = new File(this.saveFilePath);
			String[] lines = viewer.editor.getText().trim().split("\n");

			PrintWriter out = new PrintWriter(new FileOutputStream(file), true);
			for (String line : lines)
				out.println(line);

			this.saveFilePath = file.getPath();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(viewer, "Save failed!");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void actionPerformed(ActionEvent evt) {

		viewer.moveFocusToDesktop();
		JComponent j = (JComponent) evt.getSource();
		if (j.getToolTipText().equals(ActionConstant.DIS_ACTION)) {

			viewer.disconnect();
			System.exit(1);
		} else if (j.getToolTipText().equals(ActionConstant.SAVE)) {
			File f = new File(this.saveFilePath);
			if (!f.equals("") && f.exists())
				this.doSave();
			else
				this.doSaveAs();
		} else if (j.getToolTipText().equals(ActionConstant.SAVEAS)) {
			this.doSaveAs();
		} else if(j.getToolTipText().equals(ActionConstant.HELP)){
			 File f=new File("");
			   File p=new File(f.getAbsolutePath());
			   String path=p.getAbsolutePath()+"\\doc\\index.html";
			try {
				java.awt.Desktop.getDesktop().open(new File(path));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		else if(j.getToolTipText().equals(ActionConstant.SAMPLE)){
			 File f=new File("");
			   File p=new File(f.getAbsolutePath());
			   String path=p.getAbsolutePath()+"\\sample";
			try {
				java.awt.Desktop.getDesktop().open(new File(path));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		
		else if (j.getToolTipText().equals(ActionConstant.ABOUT)) {
			new AboutFrame();
		} else if (j.getToolTipText().equals(ActionConstant.OPEN)) {
			this.doOpen();
		} else if (evt.getSource() == recordAction
				&& recordAction.isSelected() == true) {
			viewer.statusBar.setText("Record action");
			Constant.recordAction = true;
			Constant.actionTime=0;
			Constant.tempAction="";
		} else if (evt.getSource() == recordAction
				&& recordAction.isSelected() == false) {
			viewer.statusBar.setText("Connected");
			Constant.recordAction = false;
			long time=System.currentTimeMillis()-Constant.actionTime;
			if(Constant.tempAction.equals("elf.type")){
				viewer.editor.append(Constant.tempStr.toString()+"\","+time+");\n");
				Constant.tempStr.delete(0,Constant.tempStr.length());
			}else{
			viewer.editor.append(","+time+");\n");
			}
			Constant.actionTime=0;
			Constant.tempAction="";
			
		}

		else if (j.getToolTipText().equals(ActionConstant.CONFIG)) {
			viewer.options.setVisible(!viewer.options.isVisible());

		} else if (j.getToolTipText().equals(ActionConstant.CAPSCREEN)) {
			viewer.statusBar.setText("Capture Screen");
			viewer.captureScreen = new CaptureScreen(viewer);
			viewer.captureScreen.setVisible(true);

		} else if (j.getToolTipText().equals(ActionConstant.START)) {
			viewer.statusBar.setText("Running......");
			run.setEnabled(false);
			viewer.logTextArea.setText("");
			running = new Thread(new EventHandler(viewer,run));
			running.start();

		} else if (j.getToolTipText().equals(ActionConstant.STOP)) {
			viewer.statusBar.setText("Stop");
			run.setEnabled(true);
			
			
			Chariotc.lt.setElf(null);
			if(running!=null)
			running.interrupt();
			

		}

		else if (j.getToolTipText().equals(ActionConstant.RE_SCREEN)) {
			viewer.statusBar.setText("Record screen");
			viewer.rec.setVisible(!viewer.rec.isVisible());

		}else if(j.getToolTipText().equals(ActionConstant.PLAY_SCREEN)){
			viewer.statusBar.setText("Record screen");
			
			VncPlayerFrame vpf=new VncPlayerFrame(viewer);
		}
		else if (j.getToolTipText().equals(ActionConstant.CLEARLOG)) {
			viewer.logTextArea.setText("");
			// viewer.clipboard.setVisible(!viewer.clipboard.isVisible());

		} 
		else if (j.getToolTipText().equals(ActionConstant.CLIPBOARD)) {
		
			 viewer.clipboard.setVisible(!viewer.clipboard.isVisible());

		}
		else if (j.getToolTipText().equals(ActionConstant.SENDCAD)) {
			try {
				final int modifiers = InputEvent.CTRL_MASK
						| InputEvent.ALT_MASK;

				KeyEvent ctrlAltDelEvent = new KeyEvent(toolBar,
						KeyEvent.KEY_PRESSED, 0, modifiers, 127);
				viewer.rfb.writeKeyEvent(ctrlAltDelEvent);

				ctrlAltDelEvent = new KeyEvent(toolBar, KeyEvent.KEY_RELEASED,
						0, modifiers, 127);
				viewer.rfb.writeKeyEvent(ctrlAltDelEvent);

			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if (j.getToolTipText().equals(ActionConstant.REFRESH)) {
			try {
				RfbProto rfb = viewer.rfb;
				rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth,
						rfb.framebufferHeight, false);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

class EventHandler implements Runnable {
	VncViewer viewer;
	JButton runButton;
	
	EventHandler(VncViewer viewer,JButton runButton) {
		this.viewer = viewer;
		this.runButton=runButton;
	}

	public void run() {
		
		VncElf vr = new VncElf();
		vr.init(viewer);
		JavaFormatScript jd = new JavaFormatScript();
		String script = viewer.editor.getText();
		UUID uuid = UUID.randomUUID();
		String fileName = "JavaTemplete" + uuid;
		fileName = fileName.replace("-", "");
		// String[] lines = viewer.editor.getText().trim().split("\n");
		String lines = viewer.editor.getText();
		String st = jd.writeScript(lines, fileName);
		viewer.editor.removeAllLineHighlights();
		try {
			Thread.sleep(1000);
			Chariotc.complie(st, fileName, vr, viewer.editor,this.runButton);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	// public void run() {
	// viewer.vc.removeMouseListener(viewer.vc);
	// viewer.vc.removeKeyListener(viewer.vc);
	// try {
	// int start = 0;
	// String text = this.viewer.editor.getText();
	// String[] line = text.split("\\n");
	//
	// for (int i = 0; i < line.length; i++) {
	// int end = start + line[i].length();
	//
	// viewer.editor.select(start, end);
	//
	// start = end + 1;
	// // viewer.editor.setCurrentLineHighlightColor(Color.black);
	//
	// if (line[i].startsWith("Mouse move ")) {
	// String[] position = line[i].split("(\\D+)");
	// int x = Integer.parseInt(position[1]);
	// int y = Integer.parseInt(position[2]);
	// int waitTime = Integer.parseInt(position[3]);
	// MouseEvent me = new MouseEvent(viewer.vc, 0, 0, 0, x, y, 0,
	// true, 0);
	// viewer.vc.processLocalMouseEvent(me, true);
	// Thread.sleep(waitTime);
	//
	// } else if (line[i].startsWith("Mouse drag")) {
	// String[] position = line[i].split("(\\D+)");
	// int fx = Integer.parseInt(position[1]);
	// int fy = Integer.parseInt(position[2]);
	// int sx = Integer.parseInt(position[3]);
	// int sy = Integer.parseInt(position[4]);
	// int waitTime = Integer.parseInt(position[5]);
	// MouseEvent me = new MouseEvent(viewer.vc,
	// MouseEvent.MOUSE_PRESSED, 0, 0, fx, fy, 1, true, 1);
	// viewer.vc.processLocalMouseEvent(me, false);
	//
	// me = new MouseEvent(viewer.vc, MouseEvent.MOUSE_DRAGGED, 0,
	// 0, sx, sy, 0, true, 1);
	// viewer.vc.processLocalMouseEvent(me, true);
	// Thread.sleep(500);
	// me = new MouseEvent(viewer.vc, MouseEvent.MOUSE_RELEASED,
	// 0, 0, sx, sy, 1, true, 1);
	// viewer.vc.processLocalMouseEvent(me, false);
	//
	// Thread.sleep(waitTime);
	// } else if (line[i].startsWith("Mouse right")) {
	// String[] position = line[i].split("(\\D+)");
	// int x = Integer.parseInt(position[1]);
	// int y = Integer.parseInt(position[2]);
	// int waitTime = Integer.parseInt(position[3]);
	// MouseEvent me = new MouseEvent(viewer.vc,
	// MouseEvent.MOUSE_PRESSED, 0, 0, x, y, 1, true, 3);
	// viewer.vc.processLocalMouseEvent(me, false);
	// me = new MouseEvent(viewer.vc, MouseEvent.MOUSE_RELEASED,
	// 0, 0, x, y, 1, true, 3);
	// viewer.vc.processLocalMouseEvent(me, false);
	//
	// Thread.sleep(waitTime);
	// }
	//
	// else if (line[i].startsWith("Mouse click ")) {
	//
	// String[] position = line[i].split("(\\D+)");
	// int x = Integer.parseInt(position[1]);
	// int y = Integer.parseInt(position[2]);
	// int waitTime = Integer.parseInt(position[3]);
	// MouseEvent me = new MouseEvent(viewer.vc,
	// MouseEvent.MOUSE_PRESSED, 0, 0, x, y, 1, true, 1);
	// viewer.vc.processLocalMouseEvent(me, false);
	// me = new MouseEvent(viewer.vc, MouseEvent.MOUSE_RELEASED,
	// 0, 0, x, y, 1, true, 1);
	// viewer.vc.processLocalMouseEvent(me, false);
	//
	// Thread.sleep(waitTime);
	// } else if (line[i].startsWith("Type ")) {
	// Pattern pattern = Pattern.compile("\"((.*?)\")+");
	// Matcher matcher = pattern.matcher(line[i]);
	// matcher.find();
	// String input = matcher.group();
	// input = input.substring(1, input.length() - 1);
	// String input_temp = input.toUpperCase();
	// for (int j = 0; j < input.length(); j++) {
	// char c = input.charAt(j);
	// char c_temp = input_temp.charAt(j);
	// KeyEvent exe = new KeyEvent(viewer.vc,
	// KeyEvent.KEY_PRESSED, 0, 0, (int) c_temp,
	// c,KeyEvent.KEY_LOCATION_STANDARD);
	//
	// viewer.vc.processLocalKeyEvent(exe);
	// exe = new KeyEvent(viewer.vc, KeyEvent.KEY_RELEASED, 0,
	// 0, (int) c_temp, c,KeyEvent.KEY_LOCATION_STANDARD);
	//
	// viewer.vc.processLocalKeyEvent(exe);
	// Thread.sleep(5);
	// }
	// String[] position = line[i].split("(\\D+)");
	// int waitTime = Integer
	// .parseInt(position[position.length - 1]);
	// Thread.sleep(waitTime);
	//
	// } else if (line[i].startsWith("Typeline ")) {
	// Pattern pattern = Pattern.compile("\"((.*?)\")+");
	// Matcher matcher = pattern.matcher(line[i]);
	// matcher.find();
	// String input = matcher.group();
	// input = input.substring(1, input.length() - 1);
	// String input_temp = input.toUpperCase();
	// for (int j = 0; j < input.length(); j++) {
	// char c = input.charAt(j);
	// char c_temp = input_temp.charAt(j);
	// KeyEvent exe = new KeyEvent(viewer.vc,
	// KeyEvent.KEY_PRESSED, 0, 0, (int) c_temp,
	// c,KeyEvent.KEY_LOCATION_STANDARD);
	//
	// viewer.vc.processLocalKeyEvent(exe);
	// exe = new KeyEvent(viewer.vc, KeyEvent.KEY_RELEASED, 0,
	// 0, (int) c_temp, c,KeyEvent.KEY_LOCATION_STANDARD);
	//
	// viewer.vc.processLocalKeyEvent(exe);
	// Thread.sleep(5);
	// }
	// KeyEvent exe = new KeyEvent(viewer.vc,
	// KeyEvent.KEY_PRESSED, 0, 0, 10);
	// viewer.vc.processLocalKeyEvent(exe);
	// exe = new KeyEvent(viewer.vc, KeyEvent.KEY_RELEASED, 0, 0,
	// 10);
	// viewer.vc.processLocalKeyEvent(exe);
	// String[] position = line[i].split("(\\D+)");
	// int waitTime = Integer
	// .parseInt(position[position.length - 1]);
	// Thread.sleep(waitTime);
	// } else if (line[i].startsWith("Press ")) {
	// String key = line[i].split(" ")[1].trim().toUpperCase();
	// String[] position = line[i].split("(\\D+)");
	// int waitTime = Integer
	// .parseInt(position[position.length - 1]);
	// KeyEvent exe = new KeyEvent(viewer.vc,
	// KeyEvent.KEY_PRESSED, 0, 0, KeyMap.getKeyValue(key));
	// viewer.vc.processLocalKeyEvent(exe);
	// exe = new KeyEvent(viewer.vc, KeyEvent.KEY_RELEASED, 0, 0,
	// KeyMap.getKeyValue(key));
	// viewer.vc.processLocalKeyEvent(exe);
	//
	// }else if (line[i].startsWith("Capture ")) {
	//
	// String[] position = line[i].split("(\\D+)");
	// int x = Integer.parseInt(position[1]);
	// int y = Integer.parseInt(position[2]);
	// int width=Integer.parseInt(position[3]);
	// int heigh=Integer.parseInt(position[4]);
	// int waitTime = Integer.parseInt(position[5]);
	// int swidth = viewer.vc.getWidth();
	// int sheight = viewer.vc.getHeight();
	//
	// BufferedImage bi = new BufferedImage(swidth, sheight,
	// BufferedImage.TYPE_INT_RGB);
	// Graphics2D g = bi.createGraphics();
	// viewer.canvasPanel.paint(g);
	// BufferedImage get= bi.getSubimage(x, y, width, heigh);
	// ImageIO.write(get, "jpg",new File( "c:\\testvnc.jpg"));
	// Thread.sleep(waitTime);
	// }else if (line[i].startsWith("Compare ")) {
	//
	// String source=line[i].split(" ")[1].trim();
	// String target=line[i].split(" ")[2].trim();
	// Finder f=new Finder(source);
	// f.find(new org.sikuli.script.Pattern(target));
	//
	// org.sikuli.script.Match m=f.next();
	// if(m!=null){
	// System.out.println("Picture is the same");
	// }else{
	// System.err.println("Picture is not the same");
	// }
	// Thread.sleep(2000);
	// }
	// else if (line[i].startsWith("ClickImage ")) {
	// String[] position = line[i].split("(\\D+)");
	// int waitTime = Integer.parseInt(position[position.length-1]);
	// String source=line[i].split(" ")[1].trim();
	//
	// int swidth = viewer.vc.getWidth();
	// int sheight = viewer.vc.getHeight();
	//
	// BufferedImage bi = new BufferedImage(swidth, sheight,
	// BufferedImage.TYPE_INT_RGB);
	// Graphics2D g = bi.createGraphics();
	// viewer.canvasPanel.paint(g);
	// ImageIO.write(bi, "jpg",new File( "c:\\testvncscreen.jpg"));
	//
	// Finder f=new Finder("c:\\testvncscreen.jpg");
	// f.find(new org.sikuli.script.Pattern(source));
	//
	// org.sikuli.script.Match m=f.next();
	// if(m!=null){
	// System.out.println(m.x+" "+m.y+" "+m.w+" "+m.h);
	// MouseEvent me = new MouseEvent(viewer.vc,
	// MouseEvent.MOUSE_PRESSED, 0, 0, m.x, m.y, 1, true, 1);
	// viewer.vc.processLocalMouseEvent(me, false);
	// me = new MouseEvent(viewer.vc, MouseEvent.MOUSE_RELEASED,
	// 0, 0, m.x, m.y, 1, true, 1);
	// viewer.vc.processLocalMouseEvent(me, false);
	//
	//
	//
	// }else{
	// System.err.println("Couldn't find the picture");
	// }
	// Thread.sleep(waitTime);
	// }
	// }
	// viewer.vc.addMouseListener(viewer.vc);
	// viewer.vc.addKeyListener(viewer.vc);
	//
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	//
	// }

}
